package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Assistant;
import com.rzj.sys_lab_assistant.entity.AssistantJob;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.entity.bo.AssistantBO;
import com.rzj.sys_lab_assistant.service.IAssistantJobService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import com.rzj.sys_lab_assistant.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-02 22:09
 */
@Controller
@RequestMapping("assistantJob")
public class AssistantJobController {

    @Autowired
    IAssistantJobService assistantJobService;

    ResultsUtils resultsUtils;

    TimeUtils timeUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "assistantJob/assistantJobAdd";
    }

    @RequestMapping("toEdit/{jobId}")
    public String toEdit(Model model, @PathVariable("jobId") int jobId) {
        model.addAttribute("job", assistantJobService.getAssistantJobById(jobId));
        return "assistantJob/assistantJobUpdate";
    }

    @RequestMapping("assistantJobList")
    public String assistantJobList(Model model, HttpSession session) { // 助理工作记录界面
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        model.addAttribute("jobList", assistantJobService.assistantJobsList(assistant.getAId())); // 根据助理登录实体类进行获取列表
        return "assistantJob/assistantJobList";
    }

    @RequestMapping("assistantJobByIdList/{aId}")
    public String assistantJobByIdList(Model model, @PathVariable("aId") int aId) { // 技术员通过所属于自己的助理列表进入对应助理的工作汇报列表界面
        model.addAttribute("jobList", assistantJobService.assistantJobsList(aId));
        return "assistantJob/assistantJobByIdList";
    }

    @RequestMapping("assistantJobByTimeList")
    public String assistantJobByTimeList(Model model, @RequestParam("date") String time, HttpSession session) throws ParseException {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("time", time);
        model.addAttribute("jobList", assistantJobService.getAssistantJobByDate(timeUtils.getDate(time), technician.getAccount())); // 根据技术员登录实体类用户名
        return "assistantJob/assistantJobByTimeList";
    }

    @RequestMapping("assistantJobAdd")
    @ResponseBody
    public Map<String, String> assistantJobAdd(@RequestParam("jobSemester") String jobSemester,
                                               @RequestParam("jobWeek") String jobWeek,
                                               @RequestParam("jobTime") String jobTime,
                                               @RequestParam("jobContent") String jobContent, HttpSession session) throws ParseException {
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        AssistantJob assistantJob = new AssistantJob(0, jobSemester, jobWeek, timeUtils.getDate(jobTime), jobContent, assistant.getAId());
        int flag = assistantJobService.saveAssistantJob(assistantJob);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantJobEdit")
    @ResponseBody
    public Map<String, String> assistantJobEdit(@RequestParam("jobId") String jobId,
                                                @RequestParam("jobSemester") String jobSemester,
                                                @RequestParam("jobWeek") String jobWeek,
                                                @RequestParam("jobTime") String jobTime,
                                                @RequestParam("jobContent") String jobContent, HttpSession session) throws ParseException {
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        AssistantJob assistantJob = new AssistantJob(Integer.valueOf(jobId), jobSemester, jobWeek, timeUtils.getDate(jobTime), jobContent, assistant.getAId());
        int flag = assistantJobService.updateAssistantJob(assistantJob);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantJobDel")
    @ResponseBody
    public Map<String, String> assistantJobDel(@RequestParam("jobId") String jobId) {

        int flag = assistantJobService.deleteAssistantJob(Integer.valueOf(jobId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }


}
